require 'net/https'
require 'rexml/document'
require_relative 'StringUtils'

class CPFService
		include StringUtils
		@@properties = JSON.load(File.open( "uploadFile.json", "r" ))
		@@CPF_GATEWAY = "#{@@properties['serverurl']}/cordys/com.eibus.web.soap.Gateway.wcp"

		attr_accessor :response, :responseXml, :request, :headers, :http, :uri

		def initialize assertion = "", gateway = @@CPF_GATEWAY
			gateway = "#{gateway}?SAMLart=#{assertion}" if !assertion.empty?
			@uri = URI.parse(gateway)
			@http = Net::HTTP.new(@uri.host, @uri.port)
			@http.use_ssl = true
			@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
		end

		def getFromResponse xpath
			responseXml.elements[xpath]
		end

		def fireService
			@response = http.start {|con| 
				con.post(uri.request_uri, request, headers) 
			}
			@responseXml = REXML::Document.new(response.body)
		end
end